library(haven)
library(tidyverse)

# Read in Raw Data ---------
data_radboud <- read_sav("Raw Data/GS Study 2_ Empathy, Censorship, and Schadenfreude_March 13, 2022_12.47.sav")
data_adam <- read_sav("Raw Data/IPRES survey_March 13, 2022_12.47.sav")
data_radboud <- data_radboud %>% drop_na(censorship_1)
data_adam <- data_adam %>% drop_na(censorship_1)

#  Create Independent Variables-----

#IRI (radboud data)
#rescaling IRI items to range from 0 to 1 
data_radboud$IRI_1 <- (data_radboud$IRI_1_1-1)/4
data_radboud$IRI_2 <- (data_radboud$IRI_1_2-1)/4
data_radboud$IRI_3 <- 1-(data_radboud$IRI_1_3-1)/4
data_radboud$IRI_4 <- 1-(data_radboud$IRI_1_4-1)/4
data_radboud$IRI_5 <- (data_radboud$IRI_1_5-1)/4
data_radboud$IRI_6 <- (data_radboud$IRI_1_6-1)/4
data_radboud$IRI_7 <- 1-(data_radboud$IRI_1_7-1)/4
data_radboud$IRI_8 <- (data_radboud$IRI_2_1-1)/4
data_radboud$IRI_9 <- (data_radboud$IRI_2_2-1)/4
data_radboud$IRI_10 <- (data_radboud$IRI_2_3-1)/4
data_radboud$IRI_11 <- (data_radboud$IRI_2_4-1)/4
data_radboud$IRI_12 <- 1-(data_radboud$IRI_2_5-1)/4
data_radboud$IRI_13 <- 1-(data_radboud$IRI_2_6-1)/4
data_radboud$IRI_14 <- 1-(data_radboud$IRI_3_1-1)/4
data_radboud$IRI_15 <- 1-(data_radboud$IRI_3_2-1)/4
data_radboud$IRI_16 <- (data_radboud$IRI_3_3-1)/4
data_radboud$IRI_17 <- (data_radboud$IRI_3_4-1)/4
data_radboud$IRI_18 <- 1-(data_radboud$IRI_3_5-1)/4
data_radboud$IRI_19 <- 1-(data_radboud$IRI_3_6-1)/4
data_radboud$IRI_20 <- (data_radboud$IRI_3_7-1)/4
data_radboud$IRI_21 <- (data_radboud$IRI_4_1-1)/4
data_radboud$IRI_22 <- (data_radboud$IRI_4_2-1)/4
data_radboud$IRI_23 <- (data_radboud$IRI_4_3-1)/4
data_radboud$IRI_24 <- (data_radboud$IRI_4_4-1)/4
data_radboud$IRI_25 <- (data_radboud$IRI_4_5-1)/4
data_radboud$IRI_26 <- (data_radboud$IRI_4_6-1)/4
data_radboud$IRI_27 <- (data_radboud$IRI_4_7-1)/4
data_radboud$IRI_28 <- (data_radboud$IRI_4_8-1)/4

data_radboud$empathic_concern <- rowMeans(data_radboud[,c("IRI_2" , "IRI_4" , "IRI_9", "IRI_14" , "IRI_18" , "IRI_20", "IRI_22")], na.rm=F)
data_radboud$perspective_taking <- rowMeans(data_radboud[,c("IRI_3" , "IRI_8" , "IRI_11" , "IRI_15" , "IRI_21" , "IRI_25" ,"IRI_28")], na.rm=F)

#IRI (Adam data)
#rescaling IRI items to range from 0 to 1 
data_adam$IRI_2 <- (data_adam$IRI_1_1-1)/4
data_adam$IRI_3 <- 1-(data_adam$IRI_1_2-1)/4
data_adam$IRI_4 <- 1-(data_adam$IRI_1_3-1)/4
data_adam$IRI_8 <- (data_adam$IRI_2_1-1)/4
data_adam$IRI_9 <- (data_adam$IRI_2_2-1)/4
data_adam$IRI_11 <- (data_adam$IRI_2_3-1)/4
data_adam$IRI_14 <- 1-(data_adam$IRI_3_1-1)/4
data_adam$IRI_15 <- 1-(data_adam$IRI_3_2-1)/4
data_adam$IRI_18 <- 1-(data_adam$IRI_3_3-1)/4
data_adam$IRI_20 <- (data_adam$IRI_3_4-1)/4
data_adam$IRI_21 <- (data_adam$IRI_4_1-1)/4
data_adam$IRI_22 <- (data_adam$IRI_4_2-1)/4
data_adam$IRI_25 <- (data_adam$IRI_4_3-1)/4
data_adam$IRI_28 <- (data_adam$IRI_4_4-1)/4

data_adam$empathic_concern <- rowMeans(data_adam[,c("IRI_2" , "IRI_4" , "IRI_9", "IRI_14" , "IRI_18" , "IRI_20", "IRI_22")], na.rm=F)
data_adam$perspective_taking <- rowMeans(data_adam[,c("IRI_3" , "IRI_8" , "IRI_11" , "IRI_15" , "IRI_21" , "IRI_25" ,"IRI_28")], na.rm=F)

# Create Experimental Condition Variables-----

#making the experimental condition (radboud)
data_radboud$protestL_speakerR = pmax(data_radboud$FL_21_DO_protestL_speakerR, data_radboud$FL_26_DO_protestL_speakerR, data_radboud$FL_27_DO_protestL_speakerR, na.rm = T)
data_radboud$protestR_speakerL = pmax(data_radboud$FL_21_DO_protestR_speakerL, data_radboud$FL_26_DO_protestR_speakerL, data_radboud$FL_27_DO_protestR_speakerL, na.rm = T)
data_radboud$LeftPisIn_SisOut <- ifelse(data_radboud$left_right_placement_1 < 5 & data_radboud$protestL_speakerR == 1, 1, 0)
data_radboud$LeftPisOut_SisIn <- ifelse(data_radboud$left_right_placement_1 < 5 & data_radboud$protestR_speakerL == 1, 1, 0)
data_radboud$RightPisOut_SisIn <- ifelse(data_radboud$left_right_placement_1 > 5 & data_radboud$protestL_speakerR == 1, 1, 0)
data_radboud$RightPisIn_SisOut <- ifelse(data_radboud$left_right_placement_1 > 5 & data_radboud$protestR_speakerL == 1, 1, 0)
data_radboud$CenterPisR.SisL <- ifelse(data_radboud$left_right_placement_1 == 5 & data_radboud$protestR_speakerL == 1, 1, 0)
data_radboud$CenterPisL.SisR <- ifelse(data_radboud$left_right_placement_1 == 5 & data_radboud$protestL_speakerR == 1, 1, 0)

data_radboud$LeftPisIn_SisOut[is.na(data_radboud$LeftPisIn_SisOut)] = 0
data_radboud$LeftPisOut_SisIn[is.na(data_radboud$LeftPisOut_SisIn)] = 0
data_radboud$RightPisOut_SisIn[is.na(data_radboud$RightPisOut_SisIn)] = 0
data_radboud$RightPisIn_SisOut[is.na(data_radboud$RightPisIn_SisOut)] = 0
data_radboud$CenterPisR.SisL[is.na(data_radboud$CenterPisR.SisL)] = 0
data_radboud$CenterPisL.SisR[is.na(data_radboud$CenterPisL.SisR)] = 0


data_radboud$protestIn_speakerOut = pmax(data_radboud$RightPisIn_SisOut, data_radboud$LeftPisIn_SisOut, na.rm = T)
data_radboud$protestOut_speakerIn = pmax(data_radboud$RightPisOut_SisIn, data_radboud$LeftPisOut_SisIn, na.rm = T)
data_radboud$protestIn_speakerOut[data_radboud$left_right_placement_1==5] = NA
data_radboud$protestOut_speakerIn[data_radboud$left_right_placement_1==5] = NA

condition_check = subset(data_radboud, select = c(FL_21_DO_protestL_speakerR, FL_26_DO_protestL_speakerR, FL_27_DO_protestL_speakerR, FL_21_DO_protestR_speakerL, FL_26_DO_protestR_speakerL, FL_27_DO_protestR_speakerL,
                                                  LeftPisIn_SisOut, LeftPisOut_SisIn, RightPisOut_SisIn, RightPisIn_SisOut, CenterPisR.SisL,CenterPisL.SisR,
                                                  left_right_placement_1, protestIn_speakerOut, protestOut_speakerIn, Closeness_to_Party, Closeness_to_Protest) )

#making the experimental condition (adam)
data_adam$protestL_speakerR = pmax(data_adam$FL_52_DO_protestL_speakerR, data_adam$FL_54_DO_protestL_speakerR, data_adam$FL_59_DO_protestL_speakerR, na.rm = T)
data_adam$protestR_speakerL = pmax(data_adam$FL_52_DO_protestR_speakerL, data_adam$FL_54_DO_protestR_speakerL, data_adam$FL_59_DO_protestR_speakerL, na.rm = T)
data_adam$protestL_speakerR <- ifelse(is.na(data_adam$protestL_speakerR) == T, 0, 1)
data_adam$protestR_speakerL <- ifelse(is.na(data_adam$protestR_speakerL) == T, 0, 1)

table(data_adam$protestR_speakerL)
table(data_adam$protestL_speakerR)

data_adam$LeftPisIn_SisOut <- ifelse(data_adam$left_right_placement_1 < 5 & data_adam$protestL_speakerR == 1, 1, 0)
data_adam$LeftPisOut_SisIn <- ifelse(data_adam$left_right_placement_1 < 5 & data_adam$protestR_speakerL == 1, 1, 0)
data_adam$RightPisOut_SisIn <- ifelse(data_adam$left_right_placement_1 > 5 & data_adam$protestL_speakerR == 1, 1, 0)
data_adam$RightPisIn_SisOut <- ifelse(data_adam$left_right_placement_1 > 5 & data_adam$protestR_speakerL == 1, 1, 0)
data_adam$CenterPisR.SisL <- ifelse(data_adam$left_right_placement_1 == 5 & data_adam$protestR_speakerL == 1, 1, 0)
data_adam$CenterPisL.SisR <- ifelse(data_adam$left_right_placement_1 == 5 & data_adam$protestL_speakerR == 1, 1, 0)

data_adam$LeftPisIn_SisOut[is.na(data_adam$LeftPisIn_SisOut)] = 0
data_adam$LeftPisOut_SisIn[is.na(data_adam$LeftPisOut_SisIn)] = 0
data_adam$RightPisOut_SisIn[is.na(data_adam$RightPisOut_SisIn)] = 0
data_adam$RightPisIn_SisOut[is.na(data_adam$RightPisIn_SisOut)] = 0
data_adam$CenterPisR.SisL[is.na(data_adam$CenterPisR.SisL)] = 0
data_adam$CenterPisL.SisR[is.na(data_adam$CenterPisL.SisR)] = 0

data_adam$protestIn_speakerOut = pmax(data_adam$RightPisIn_SisOut, data_adam$LeftPisIn_SisOut, na.rm = T)
data_adam$protestOut_speakerIn = pmax(data_adam$RightPisOut_SisIn, data_adam$LeftPisOut_SisIn, na.rm = T)
data_adam$protestIn_speakerOut[data_adam$left_right_placement_1==5] = NA
data_adam$protestOut_speakerIn[data_adam$left_right_placement_1==5] = NA

condition_check = subset(data_adam, select = c(FL_52_DO_protestL_speakerR, FL_54_DO_protestL_speakerR, FL_59_DO_protestL_speakerR, FL_52_DO_protestR_speakerL, FL_54_DO_protestR_speakerL, FL_59_DO_protestR_speakerL,
                                               LeftPisIn_SisOut, LeftPisOut_SisIn, RightPisOut_SisIn, RightPisIn_SisOut, CenterPisR.SisL,CenterPisL.SisR,
                                               left_right_placement_1, protestIn_speakerOut, protestOut_speakerIn, Closeness_to_Party, Closeness_to_Protest) )

# Create Dependent Variables-----

#dependent variables (Radboud)
data_radboud$censorship_3 <- 8 - data_radboud$censorship_3
data_radboud$censorship_4 <- 8 - data_radboud$censorship_4

Censorship1 <- subset(data_radboud, select = c(censorship_1,censorship_3,censorship_4) )
Punish1 <- subset(data_radboud, select = c(punishment_1, punishment_2) )
Sympath1 <- subset(data_radboud, select = c(sympathy_1, sympathy_2) )
Schaden1 <- subset(data_radboud, select = c(schadenfreude_1, schadenfreude_2) )

data_radboud$censorship <- rowMeans(data_radboud[,c("censorship_1" , "censorship_3", "censorship_4")], na.rm=F)
data_radboud$censorship <- 8 - data_radboud$censorship
data_radboud$punishment <- rowMeans(data_radboud[,c("punishment_1" , "punishment_2")], na.rm=F)
data_radboud$punishment <- 8 - data_radboud$punishment

data_radboud$sympathy <- rowMeans(data_radboud[,c("sympathy_1" , "sympathy_2")], na.rm=F)
data_radboud$schadenfreude <- rowMeans(data_radboud[,c("schadenfreude_1" , "schadenfreude_2")], na.rm=F)

#dependent variables (adam)
data_adam$censorship_3 <- 8 - data_adam$censorship_3
data_adam$censorship_4 <- 8 - data_adam$censorship_4

Censorship2 <- subset(data_adam, select = c(censorship_1,censorship_3,censorship_4) )
Punish2 <- subset(data_adam, select = c(punishment_1, punishment_2) )
Sympath2 <- subset(data_adam, select = c(sympathy_1, sympathy_2) )
Schaden2 <- subset(data_adam, select = c(schadenfreude_1, schadenfreude_2) )

data_adam$censorship <- rowMeans(data_adam[,c("censorship_1" , "censorship_3", "censorship_4")], na.rm=F)
data_adam$censorship <- 8 - data_adam$censorship
data_adam$punishment <- rowMeans(data_adam[,c("punishment_1" , "punishment_2")], na.rm=F)
data_adam$punishment <- 8 - data_adam$punishment
data_adam$sympathy <- rowMeans(data_adam[,c("sympathy_1" , "sympathy_2")], na.rm=F)
data_adam$schadenfreude <- rowMeans(data_adam[,c("schadenfreude_1" , "schadenfreude_2")], na.rm=F)

# Create Control Variables-----
data_radboud$Perceived_Realism_2 <- 8 - data_radboud$Perceived_Realism_2
realism <- subset(data_radboud, select = c(Perceived_Realism_1, Perceived_Realism_2) )
data_radboud$ClosenessProtestGroup <- data_radboud$Closeness_to_Party + data_radboud$Closeness_to_Protest
data_radboud$Language_EN <- 0
data_radboud$UvA <- 0

data_adam$Perceived_Realism_2 <- 8 - data_adam$Perceived_Realism_2
realism <- subset(data_adam, select = c(Perceived_Realism_1, Perceived_Realism_2) )
data_adam$ClosenessProtestGroup <- data_adam$Closeness_to_Party + data_adam$Closeness_to_Protest
data_adam$Language_EN <- ifelse(data_adam$UserLanguage == "EN", 1, 0)
data_adam$UvA <- 1

# Combine Amsterdam And Radboud data
data_radboud <- subset(data_radboud, select = c(empathic_concern, perspective_taking, 
                                               censorship, punishment, sympathy,schadenfreude,
                                               protestIn_speakerOut, protestOut_speakerIn,
                                               protestR_speakerL, protestL_speakerR,
                                               LeftPisIn_SisOut, LeftPisOut_SisIn,RightPisOut_SisIn,RightPisIn_SisOut,
                                               CenterPisR.SisL, CenterPisL.SisR, left_right_placement_1,
                                               Closeness_to_Party, Closeness_to_Protest, ClosenessProtestGroup,
                                               Perceived_Realism_1,Perceived_Realism_2,
                                               Progress,Duration__in_seconds_,Finished,Language_EN, UvA))

data_adam <- subset(data_adam, select = c(empathic_concern, perspective_taking, 
                                         censorship, punishment, sympathy,schadenfreude,
                                         protestIn_speakerOut, protestOut_speakerIn,
                                         protestR_speakerL, protestL_speakerR,
                                         LeftPisIn_SisOut, LeftPisOut_SisIn,RightPisOut_SisIn,RightPisIn_SisOut,
                                         CenterPisR.SisL, CenterPisL.SisR, left_right_placement_1,
                                         Closeness_to_Party, Closeness_to_Protest, ClosenessProtestGroup,
                                         Perceived_Realism_1,Perceived_Realism_2,
                                         Progress,Duration__in_seconds_,Finished,Language_EN, UvA))
data <- rbind(data_radboud, data_adam)


# Asssigning center placement respondents to condition groups ---------
data$protestIn_speakerOut_IncCenter <- data$protestIn_speakerOut
data$protestIn_speakerOut_IncCenter[data$left_right_placement_1==5 & data$ClosenessProtestGroup>7.5] = 1
data$protestIn_speakerOut_IncCenter[data$left_right_placement_1==5 & data$ClosenessProtestGroup<7.5] = 0
data$protestOut_speakerIn_IncCenter <- 1 - data$protestIn_speakerOut_IncCenter

data$Perceived_Realism <- data$Perceived_Realism_1 + data$Perceived_Realism_2
data$Perceived_Realism01 <- (data$Perceived_Realism-2)/12
data$Perceived_UNRealism01 <- 1 - data$Perceived_Realism01

data$Right_leaning <- ifelse(data$left_right_placement_1 > 5, 1, 0)
data$Left_leaning <- ifelse(data$left_right_placement_1 < 5, 1, 0)
data$Center_leaning <- ifelse(data$left_right_placement_1 == 5, 1, 0)

data$ClosenessProtestGroup_01 <- (data$ClosenessProtestGroup - 2)/8
data$DistanceProtest_01 <- 1 - data$ClosenessProtestGroup_01

#saving final dataset
save(data, file = "Data/study_2.RData")


